package baseGame;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import controllers.GameControl;
import drawableClasses.FindGame;
import drawableClasses.MainMenu;
import enums.GameScreenStatus;

public class Game extends JFrame implements MouseMotionListener, MouseListener, KeyListener{
	private static final long serialVersionUID = 1L;
	
	private GameScreenStatus screenStatus;
	
	private GameControl controller = new GameControl();
	
	private int fps = 30;
	private int windowW = 1024;
	private int windowH = 768;
	private BufferedImage backBuffer;
	
//	private ImageIcon backgroundImage;
	
	private boolean[] keys = new boolean[1024];
	
//	public Game (){
//		
//	} 
	
	public static void main(String[] args){
		Game game = new Game();
		game.run();
	}
	
	public void run(){
		initialize();
		while(true){
			update();
			draw();
			try {
				Thread.sleep(1000/fps);
			} catch (InterruptedException e) {
				System.out.println("An error ocurred in Thread");
			}
		}
	}
	
	// BASIC METHODS
	public void initialize(){
		screenStatus = controller.getCurrentScreen();
		setTitle("Challenge of Safe Chemical");
//		setState(MAXIMIZED_BOTH);
		setSize(windowW, windowH);
		setResizable(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);
		setVisible(true);
		backBuffer = new BufferedImage(windowW, windowH, BufferedImage.TYPE_INT_RGB);

		addKeyListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public void update(){
//		backgroundImage = getBackgroundImage();
//		if(JFrame.Mi == getExtendedState()){
//			System.out.println("FUNFOU!");
//		}
		screenStatus = controller.getCurrentScreen();
		controller.update();
	}
	
	public void draw(){
		Graphics g = getGraphics();
		Graphics bbg = backBuffer.getGraphics();
		bbg.drawImage(getBackgroundImage().getImage(),0,0,windowW,windowH,this);
		//====================================================================================================================================//
		
		screenDraw(bbg);
		
		//====================================================================================================================================//
		g.drawImage(backBuffer, 0, 0, this);
	}
	// END BASIC METHODS
	
	
	//KEYBOARD METHODS
	@Override
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		keys[key] = true;
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		keys[key] = false;
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	//END KEYBOART METHODS
	
	
	// MOUSe METHODS
	@Override
	public void mouseClicked(MouseEvent e) {
		controller.clickedAction(e);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-gene0rated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		switch (screenStatus) {
		case MainMenu:
			((MainMenu)controller.getCurrentGame()).update(e);
			break;

//		case ErrorsGame:
//			break;
//
//		case FindGame:
//			break;
//
//		case Config:
//			
//			break;
//
//		case QuizGame:
//			
//			break;
//
//		case Ranking:
//			
//			break;
		}
		
	}
	// EDN MOUSe METHODS
	
	public ImageIcon getBackgroundImage() {
		ImageIcon returnImage = null;
		
		switch (screenStatus) {
			case MainMenu:
				returnImage = controller.getCurrentGame().getBackgroundImage();
				break;

			case ErrorsGame:
				
				break;

			case FindGame:
				returnImage = controller.getCurrentGame().getBackgroundImage();
				break;

			case Config:
				
				break;

			case QuizGame:
				
				break;

			case Ranking:
				
				break;
		}
		
		return returnImage;
	}

	private void screenDraw(Graphics bbg) {
		switch (screenStatus) {
		case MainMenu:
			((MainMenu)controller.getCurrentGame()).draw(bbg, this);
			break;

		case ErrorsGame:
			break;

		case FindGame:
			((FindGame)controller.getCurrentGame()).draw(bbg, this);
			break;

		case Config:
			
			break;

		case QuizGame:
			
			break;

		case Ranking:
			
			break;
		}
	}
}
